﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using ApplProtocol;

public class RoomSceneUI : MonoBehaviour
{
    public Text textRoomHeader;
    public Text textBtnEnableRoom;
    public Text textBtnRoomAction;

    public Image roomDetailBackGround;

    public Button btnRoomAction;
    public Button btnRoomEnable;
    public GridLayoutGroup gridLayout;

    public Transform  leftHeroTransform;
    public Transform rightHeroTransform;

    public GameObject heroModel;

    private SpriteRenderer sr_LeftHeroReady;
    private SpriteRenderer sr_RightHeroReady;
    private Dictionary<string, Delegate> map = new Dictionary<string, Delegate>();

    private void Awake()
    {
        map.Add(ApplProtocolType.roomList.ToString(), new Action(UpdateRoomList));
        map.Add(ApplProtocolType.beginGame.ToString(), new Action(BeginGame));
    }

    private void Start()
    {
        //BtnEnableRoomOnclicked();
        //btnRoomActionOnCliked();
    }

    public void BtnEnableRoomOnclicked()
    {
        string pclStr = PclUtils.Load(Client.isInRoom ? ApplProtocolType.quitRoom : ApplProtocolType.createRoom);

        SocketObject.SendString(pclStr);

        Debug.Log(pclStr);


        Client.activeRoom = null;
        if (rightHeroTransform.childCount > 0) Destroy(rightHeroTransform.GetChild(0).gameObject);
        if (leftHeroTransform.childCount > 0) Destroy(leftHeroTransform.GetChild(0).gameObject);

        Client.isInRoom = !Client.isInRoom;
        Client.isReady = false;
        if (Client.isInRoom) Client.isLeft = true;
        textBtnEnableRoom.text = Client.isInRoom ? "离开房间" : "创建房间" ;
    }

    public void BtnRoomDetailOnCliked(Room room)
    {
        if(Client.isInRoom && room != Client.activeRoom)
        {
            BtnEnableRoomOnclicked();
        }
        else if(room == Client.activeRoom) { return; }

        Client.activeRoom = room;

        btnRoomAction.interactable = true;
        textBtnRoomAction.text = "进入房间";
    }

    public void btnRoomActionOnCliked()
    {
        string pclStr = "";
        if (!Client.isInRoom)
        {
            pclStr = PclUtils.Load(ApplProtocolType.enterRoom, Client.activeRoom.OwnerName);

            textBtnRoomAction.text = "准备";
        }
        else
        {
            pclStr = PclUtils.Load(ApplProtocolType.setReady, (!Client.isReady).ToString());
            textBtnRoomAction.text = Client.isReady ? "准备" : "取消准备";
            Client.isReady = !Client.isReady;
            btnRoomEnable.interactable = !btnRoomEnable.interactable;
        }
        SocketObject.SendString(pclStr);
        Client.isInRoom = true;
    }

    private void Update()
    {
        if (!PclObject.handled)
        {
            Debug.Log(PclObject.type);
            if (map.ContainsKey(PclObject.type))
            {
                map[PclObject.type].DynamicInvoke();
            }
            PclObject.handled = true;
        }

        if(Client.activeRoom != null)
        {
            bool noLeftModel   = leftHeroTransform.transform.childCount == 0;
            bool noRightModel  = rightHeroTransform.transform.childCount == 0;

            if (Client.activeRoom.ExistLeftPlayer && noLeftModel)
            {
                GameObject hero = Instantiate(heroModel, leftHeroTransform);
                hero.transform.Find("PlayerNameText").GetComponent<Text>().text 
                    = Client.activeRoom.LeftPlayerName;
                sr_LeftHeroReady = hero.transform.Find("ready").GetComponent<SpriteRenderer>();
            }
            else if(!Client.activeRoom.ExistLeftPlayer && !noLeftModel)
            {
                Destroy(leftHeroTransform.GetChild(0).gameObject);
            }
            else if (!noLeftModel)
            {
                sr_LeftHeroReady.color =
                    new Vector4(1, 1, 1, Client.activeRoom.LeftPlayerReady ? 1 : 0);
            }

            if (Client.activeRoom.ExistRightPlayer && noRightModel)
            {
                GameObject hero = Instantiate(heroModel, rightHeroTransform);

                Vector3 scale = hero.transform.Find("HeroModelSprite").localScale;
                hero.transform.Find("HeroModelSprite").localScale = new Vector3(-scale.x, scale.y, scale.z);

                hero.transform.Find("PlayerNameText").GetComponent<Text>().text 
                    = Client.activeRoom.RightPlayerName;
                sr_RightHeroReady = hero.transform.Find("ready").GetComponent<SpriteRenderer>();
            }
            else if (!Client.activeRoom.ExistRightPlayer && !noRightModel)
            {
                Destroy(rightHeroTransform.GetChild(0).gameObject);
            }
            else if (!noRightModel)
            {
                sr_RightHeroReady.color = 
                    new Vector4(1, 1, 1, Client.activeRoom.RightPlayerReady? 1 : 0);
            }

            //if (Client.activeRoom.OwnerName == Client.userName) textBtnEnableRoom.text = "离开房间";

            textRoomHeader.text = Client.activeRoom.OwnerName + "的房间";
        }
        textRoomHeader.gameObject.SetActive(Client.activeRoom != null);
        roomDetailBackGround.color = new Vector4(1, 1, 1, Client.activeRoom == null ? 0.4f : 1f);
        textBtnEnableRoom.text = Client.isInRoom ? "离开房间" : "创建房间";
        btnRoomEnable.interactable = !Client.isReady;
        btnRoomAction.interactable = 
            (Client.activeRoom != null) && (Client.isInRoom || !Client.activeRoom.IsFull);
    }

    void UpdateRoomList()
    {
        RoomManagement.UpdateRoomList(this);
    }

    void BeginGame()
    {
        Client.state = Client.State.inGame;
        SceneManager.LoadScene(2);
    }


}
